package com.yangzc.lion.sys.organization.service.spring;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yangzc.lion.common.model.IBaseModel;
import com.yangzc.lion.common.service.BaseServiceImp;
import com.yangzc.lion.sys.organization.model.SysOrgPerson;
import com.yangzc.lion.sys.organization.service.IKmssPasswordEncoder;
import com.yangzc.lion.sys.organization.service.ISysOrgPersonService;
import com.yangzc.lion.sys.security.encrypt.impl.MD5Encoder;
import com.yangzc.lion.sys.util.SecureUtil;
import com.yangzc.lion.sys.util.StringUtil;

public class SysOrgPersonServiceImp extends BaseServiceImp<SysOrgPerson>
		implements ISysOrgPersonService {
	

	private IKmssPasswordEncoder passwordEncoder;
	
	public IKmssPasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	public void setPasswordEncoder(IKmssPasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}
	
	private boolean isInitPassword = true;

	public SysOrgPerson findByLoginName(String loginName) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("loginName", loginName);
		param.put("fdIsAvailable", true);
		List<SysOrgPerson> personList = this
				.find("from SysOrgPerson p where p.fdLoginName = :loginName and p.fdIsAvailable =:fdIsAvailable",
						param);
		if (personList != null && !personList.isEmpty()) {
			return personList.get(0);
		}
		return null;
	}

	public SysOrgPerson findByEmail(String fdEmail) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("fdEmail", fdEmail);
		param.put("fdIsAvailable", true);
		List<SysOrgPerson> personList = this
				.find("from SysOrgPerson p where p.fdEmail = :fdEmail and p.fdIsAvailable =:fdIsAvailable",
						param);
		if (personList != null && !personList.isEmpty()) {
			return personList.get(0);
		}
		return null;
	}
	
	
	@Override
	public String add(SysOrgPerson modelObj) throws RuntimeException {
		SysOrgPerson person = (SysOrgPerson) modelObj;
		String fdNewPassword = person.getFdNewPassword();
		if (StringUtil.isNotNull(fdNewPassword)) {
			person.setFdPassword(passwordEncoder.encodePassword(fdNewPassword,person.getFdLoginName()));
			if (isInitPassword) {
				person.setFdInitPassword(SecureUtil
						.BASE64Encoder(fdNewPassword));
			}
		}
		return super.add(person);
	}
	
	@Override
	public void update(SysOrgPerson modelObj) throws RuntimeException {
		SysOrgPerson person = (SysOrgPerson) modelObj;
		/*String fdNewPassword = person.getFdNewPassword();
		String fdId = modelObj.getFdId();
		SysOrgPerson oldPerson = this.findByPrimaryKey(fdId);
		if (StringUtil.isNotNull(fdNewPassword)&&!fdNewPassword.equals(person.getFdPassword())) {
			person.setFdPassword(new MD5Encoder().encrypt(fdNewPassword, person.getFdLoginName()));
			if (isInitPassword) {
				person.setFdInitPassword(SecureUtil
						.BASE64Encoder(fdNewPassword));
			}
		}*/
		super.update(person);
	}
}
